package com.example.app_back.controller;

import com.example.app_back.Response;
import com.example.app_back.dto.GetUserDto;
import com.example.app_back.dto.AddUserDto;
import com.example.app_back.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/user/{id}")
    public Response<GetUserDto> getUserById(@PathVariable long id) {
        return Response.newSuccess(userService.getUserById(id));
    }

    @PostMapping("/user")
    public Response<Long> addNewUser(@RequestBody AddUserDto adduserdto) {
        return Response.newSuccess(userService.addNewUser(adduserdto));
    }

    @DeleteMapping("/user/{id}")
    public void deleteUserById(@PathVariable long id) {
        userService.deleteUserById(id);
    }
    @PutMapping("/user/{id}")
    public Response<GetUserDto>updateUserById(@PathVariable long id,@RequestParam(required = false) String name,
                                              @RequestParam(required = false) String password)
    {
     return Response.newSuccess(userService.updateUserById(id,name,password));
    }
}
